﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace prjYan.Forum.Areas.Admin.Controllers
{
    [Authorize(Roles=YanForum.Engine.ViewModels.UserRoles.Admin)]
    public class CommentAdminController : prjYan.Forum.Controllers.BaseController
    {
        //
        // GET: /Admin/CommentAdmin/
        private static int PageSize = 20;

        // Danh sách comment
        // Duyệt comment
        // Search comment theo ngày, trạng thái(đã duyệt, chưa duyệt)
        // Chức năng duyệt hàng loạt

        public ActionResult Index(YanForum.Engine.ViewModels.ManageCommentOptions option, int p = 1)
        {
            option.PageIndex = p;
            option.PageSize = PageSize;

            var model = YanForum.Engine.Services.Comment.GetManageListComment(option);

            ViewBag.Message = TempData["MessageComment"];

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection frm)
        {

            var viewModel = new YanForum.Engine.ViewModels.ManageCommentIndex
            {
                ListComment = new List<YanForum.Engine.ViewModels.ManageCommentItem>(),
                Options = new YanForum.Engine.ViewModels.ManageCommentOptions()
            };

            UpdateModel(viewModel);

            List<YanForum.Engine.ViewModels.ManageCommentItem> checkedEntries = viewModel.ListComment.Where(c => c.IsChecked).ToList();
            var lstIdComment = string.Join(",",checkedEntries.Select(r=>r.Id));


            switch (viewModel.BulkAction)
            {
                case YanForum.Engine.ViewModels.CommentBulkAction.Approved:
                    //Duyệt comment
                    YanForum.Engine.Services.Comment.CommentApproved(lstIdComment,GetUserId);
                    TempData["MessageComment"] = string.Format("{0} comment đã được duyệt!", checkedEntries.Count());

                    break;
                case YanForum.Engine.ViewModels.CommentBulkAction.Delete:
                    //Xóa comment
                    YanForum.Engine.Services.Comment.CommentDelete(lstIdComment, GetUserId);
                    TempData["MessageComment"] = string.Format("{0} comment đã xóa!",checkedEntries.Count());
                    break;
            }

            return Redirect(viewModel.ReturnUrl);
        }

        public PartialViewResult _partialCommentSearch()
        {
            return PartialView();
        }


        #region Ajax

        public JsonResult AjaxDeleteComment(string commentId)
        {
            try
            {
                YanForum.Engine.Services.Comment.CommentDelete(commentId,GetUserId);
                return Json(new { status = true });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message});
            }
        }

        public JsonResult AjaxApprovedComment(string commentId)
        {
            try
            {
                YanForum.Engine.Services.Comment.CommentApproved(commentId, GetUserId);
                return Json(new { status = true });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, message = ex.Message });
            }
        }

        public JsonResult AjaxCountAllCommentNotApproved()
        {
            try
            {
                int totalCount = YanForum.Engine.Services.Comment.CountAllComemntByApproved(0);
                return Json(new { total = totalCount });
            }
            catch (Exception ex)
            {
                return Json(new { total = 0 });
            }
        }

        #endregion

    }
}
